home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 19 / Mac Magazin and MacEasy Magazine CD - Issue 19.iso / Wissenschaft & Technik / Open Prolog / Documents / Open Prolog Extras < prev    next >
Text File  |  1995-12-19  |  17KB  |  331 lines

  1. The version of Open Prolog to which this document refers is given in the Get Info…  information for this document.
  2.  
  3. • Open Prolog Extras
  4. Some of the useful external predicates in Open Prolog are listed below.
  5. Check for the existence of these predicates as described in Open Prolog Info -
  6. it's always possible for the documentation to be out of date.
  7. Mail us if you see something you can't understand or use - that way,
  8. we might fix the documentation…
  9.  
  10. • Multiple Choice
  11.  
  12. • An external predicate has been added: 'system$multiple$choice'/15.
  13. It is also presented as a sample external predicate, written in MPW pascal.
  14.  
  15. 'system$multiple$choice'/15 presents a dialog containing an OK button, an optional second button, an optional message and up to five edit boxes, each with its own prompt.
  16. It returns the text of the button pressed.
  17.  
  18. 'system$multiple$choice'(DialogNameAtom,MessageString,OKButtonNameAtom,SecondButtonNameAtom,ReplyAtom,
  19. Prompt1String,Return1String,
  20. Prompt2String,Return2String,
  21. Prompt3String,Return3String,
  22. Prompt4String,Return4String,
  23. Prompt5String,Return5String).
  24.  
  25. If the SecondButtonNameAtom is '', then the button doesn't appear.
  26. The MessageString and the PromptStrings and ReturnStrings are all lists of integers, each integer being the ASCII of the character (useful for parsing with DCGs).
  27. If a PromptString is [] (i.e. nothing) then neither it nor its corresponding edit box will appear on the dialog. Thus you can control the number of multiple answers available.
  28.  
  29. Here is an example:
  30. 'system$multiple$choice'(
  31.         'Sample Dialog…',
  32.         "This is a sample message",
  33.         'Oui',
  34.         'Non',
  35.         ReplyAtom,
  36.          "Prompt for a string here",Return1String,
  37.          "",Return2String,    %invisible second choice
  38.          "…and here too…",Return3String,
  39.          "",Return4String, %invisible fourth choice
  40.          "…finally, here.",Return5String).
  41.  
  42. • Menus
  43. ◊ Adding & Manipulating Menus
  44. To add a menu, or to change an existing menu, use the following call:
  45.  
  46. 'system$menu'(set,MenuName,MenuItemName,Kind,Enable,
  47.                         Icon,KeyEquivalent,MarkChar,MenuNumber,ItemNumber).
  48.  
  49. %a MenuItemName of '' will not add anything to the list of the menu's items, but will add the menu to the list of menus if necessary.
  50. %always use 'set' or 'delete' for the first argument.
  51. %enter 'standard' or 'hierarchical' for the kind field.
  52. %Enter 'enabled'  or 'disabled' or '' in the Enable field.
  53. %Enter Icon Number or 0 in Icon Field
  54. %Enter character code for Keyboard Equivalent in KeyEquivalent Field (only works for a new menu).
  55. %Enter character code for Marking Character in MarkChar Field.
  56. %Menu Number & Item Number are returned.
  57.  
  58. 'system$menu' can be used to enable & disable existing menu items.
  59. The system adds 256 to the icon number you provide, if it's not 0.
  60. (Supply your own icons using something like ResEdit.)
  61. example:
  62.  
  63. J is "",K is "Y",'system$menu'(set,'Trixi','First Menu 2',standard,enabled,1,K,J,Menu,Item).
  64.  
  65. SUGGESTION - set the font for this window to Chicago (the standard menu bar font), and experiment with different values for J above (look in KeyCaps for reference - e.g. control-R gives the tick mark ).
  66. When the menu is selected by the user, Open Prolog looks for a clause
  67. with a head of the form:
  68. 'system$menu$hook'(MenuName,MenuItemName)
  69. If it is found, the clause is executed.
  70.  
  71. ° Hierarchical Menus
  72. The Command Key field and the Mark Char field are re-used to implement hierarchical menus. To hook up hierarchical menus, the Markchar number of the menu item that points to the hierarchical menus must be the same as the hierarchical menu's Menunumber. So, you can define the hierarchical menu items first, getting back the menu's menunumber; then define the menu item that points to it as a hierarchical menu pointer (by making its command character number 27), and 'hook it up' to the previously defined hierarchical menu by defining its markchar number to be the same as the hierarchical menu's number. Complicated huh?
  73.  
  74. Notice that when a hierarchical menu item is selected, it is the *menu's* name and the menu item name that are returned - see the example below.
  75.  
  76. Here's an example that also demonstrates the 'system$menu$hook'. Select it all and press the ENTER key:
  77.  
  78. 'system$owner$name'(Y),(Y='',You='Oh Wise User';Y=You),
  79. 'system$menu'(set,'Greeting',You,hierarchical,enabled,0,0,0,MenuNumber,_),
  80. 'system$menu'(set,'Greeting','-',hierarchical,disabled,0,0,0,MenuNumber,_),
  81. 'system$menu'(set,'Greeting','Mike',hierarchical,enabled,0,0,0,MenuNumber,_),
  82. 'system$menu'(set,'Greeting','Nóirín',hierarchical,enabled,0,0,0,MenuNumber,_),
  83. 'system$menu'(set,'Greeting','-',hierarchical,disabled,0,0,0,MenuNumber,_),
  84. 'system$menu'(set,'Greeting','Clare',hierarchical,enabled,0,0,0,MenuNumber,_),
  85. 'system$menu'(set,'Greeting','Katie',hierarchical,enabled,0,0,0,MenuNumber,_),
  86. 'system$menu'(set,'Greeting','Alison',hierarchical,enabled,0,0,0,MenuNumber,_),
  87. 'system$menu'(set,'Sample1','Example',standard,enabled,0,27,MenuNumber,Menu,Item),
  88. assert(('system$menu$hook'('Greeting',X) :- write('Hi '),write(X),nl)).
  89.  
  90. ◊ Deleting Menu Items & Menus
  91. 'system$menu'(delete,Menu,MenuItem,_,_,_,_,_,_,_).
  92.  
  93. If the MenuItem is '', the whole menu is deleted. Be careful about deleting the standard menus - they are linked to Open Prolog by position, not by text or appearance.
  94.  
  95. For some special purposes, you may wish to delete the Prolog menu; that's fine, but you'd also be deleting the command-(.) menu item which is used for stopping wayward program execution. You can redefine the command-(.) menu item to suit your own purposes, as long as you make sure its command key equivalent is the dot.
  96.  
  97. • Status Bar
  98. The status bar is available to your program as a convenient display for the state of your program. Users are happier when they know something is happening. The rotating cursor indicates that Open Prolog is running, and you can get your program to display ongoing information on the status bar.
  99. There are three resources available to your program:
  100. (1) The status box at the extreme left - normally it contains the prompt ?- but it may contain 'Running…' 'Results…' 'mm' or 'gc' indicating different activities (mm means memory management, gc means garbage collection). The debugger uses it as well. You can use it yourself without interfering with these other uses of it.
  101. (2) The message box, from the right hand side of the window leftwards. This may contain a larger string of text than the status box.
  102. (3) The progress indicator. This is a 'thermometer' - a rectangle in which you can set the fraction of it showing gray. For example, you see this a file is being consulted or reconsulted.
  103.  
  104. You can push, set (i.e. change) or pull messages to or from the status and message boxes, and you can show, hide and set the progress indicator.
  105.  
  106. ° Status & Message Stuff
  107.  
  108. Example:
  109. 'system$push$display'(message,center,'Hi',' ',there,' user'),
  110. 'system$delay$ticks'(100),
  111. 'system$pop$display'(message).
  112.  
  113. First argument: status or message
  114. Second argument: left, centre, center or right
  115. Third, Fourth, Fifth and Sixth Arguments: these will be concatenated and displayed.
  116.  
  117. Pushing a message hides the old message, so that when you pop the message, the old message reappears.
  118. Setting a message replaces the text of the old message, which cannot subsequently be recovered.
  119.  
  120. ° Progress Indicator stuff
  121.  
  122. 'system$show$progress'. %display progress thermometer
  123. 'system$hide$progress'. %hide display thermometer
  124.  
  125. 'system$set$progress'(A,B).   % set gray part of thermometer as a fraction of the rectangle equal to the ratio A/B. A and B must be integers.
  126.  
  127. Previous values of A and B are lost.
  128.  
  129. Note: if your application's execution is characterised by processing a file, then by using the 'system$get$file$size' and 'system$get$fpos' predicates (see below) in conjunction with the progress indicator, you can give the user an idea of how much progress is being made. Here is an example where calls to the get0 predicate could be replaced by get0WithProgress:
  130.  
  131. get0WithProgress(X) :-
  132.         seeing(user),!, %don't display for input from the user…
  133.         get0(X).
  134. get0WithProgress(X) :-
  135.         seeing(File),
  136.         'system$get$file$size'(File,Size,0),
  137.         'system$get$fpos'(Pos),
  138.         'system$set$progress'(Pos,Size),
  139.         get0(X).
  140.  
  141.  
  142. • Alerts and Confirmations
  143.  
  144. 'system$alert'(alertMessage1,alertMessage2,alertMessage3,alertMessage4).
  145. %must be atomic terms. They are concatenated & displayed.
  146.  
  147. 'system$confirm'(message1,message2,message3,message4).
  148. %must be atomic terms. They are concatenated & displayed.
  149. Succeeds or fails depending on Yes or No button.
  150.  
  151. • Information
  152.  
  153. 'system$owner$name'(Name). % from Chooser or Sharing Setup. It used to be called
  154.                                                %'system$username'(X).
  155.  
  156. 'system$machine$name'(Name). %System 7 only - from Sharing Setup
  157.  
  158. 'system$machine$kind'(Name). %System 7 only - e.g. PowerBook 140.
  159.  
  160. 'system$show$about' %Shows the About Open Prolog… box
  161. 'system$hide$about' %Shows the About Open Prolog… box
  162.  
  163. 'toolbox$GetIndString'(Atom,ResourceNumber,Index). %same as the Mac
  164. Toolbox GetIndString, where the string is turned into an atom.
  165.  
  166. 'system$predicate'(Name,Term,Kind). %all normal, built-in and external predicates (see example in Open Prolog Info).
  167.  
  168. • Time & Date
  169.  
  170. 'system$seconds'(Seconds). %Since 1/1/1904 - negative but don't worry. Use it for date and time, as shown below:
  171. 'system$date'(Seconds,Date). %Converts Seconds to Date
  172. 'system$time'(Seconds,Time). %Converts Seconds to Time
  173. 'system$delay$ticks'(Ticks). %stop execution for the specified number of Ticks - a tick is about 1/60th of a second.
  174.  
  175. • Random Numbers
  176.  
  177. 'system$random'(Range,RandomNumber). %range is always input
  178. %range is from 0 to the predecessor of Range.
  179. If Range is positive, then the numbers will go from 0 to Range-1, inclusive.
  180. If Range is negative, then the numbers will go from 0 to Range+1, inclusive.
  181. It doesn't make sense to ask for random numbers in the range of -1, 0 or +1.
  182.  
  183. %use command-dot to exit from the following example:
  184. repeat,'system$random'(10,RandomNumber),write(RandomNumber),write(' '),ttyflush,fail. 
  185.  
  186. • Sound
  187.  
  188. macintalk(Phrase,Volume).  %This will speak the text of the phrase, and needs MacinTalk to work. 
  189. The Phrase must be an atom or a number. 
  190. Pass a Volume of 0 - 7.  A volume of 0 means the current sound volume control setting
  191. will be used.
  192. Due to a bug in Macintalk, all other sounds are disabled!
  193. KidsTime™ from Great Wave Software has a way of getting round this. If anyone knows how, please let us know.
  194.  
  195. eg. 
  196. 'system$owner$name'(X),macintalk('Hi',7),macintalk(X,7).
  197.  
  198. play(SoundName,Volume) will play any Type 1 'snd ' resource.
  199. Sounds recorded with the ‘Sound’ Control Panel Device and 
  200. all the sounds for the System Bell are snds of this type.
  201. To play them, give their names exactly as they appear in
  202. the ‘Sound’ Control Panel Device.
  203. Be careful to get spacing and punctuation exactly right.
  204. If the sound can't be found, or is too big to fit into memory,
  205. the call will fail,
  206. Pass a Volume of 0 - 7.  A volume of 0 means the current sound volume control setting
  207. will be used.
  208.  
  209. E.g.
  210. play('Simple Beep',5).
  211.  
  212. The snd resources can be resident in any open window, the Worksheet,
  213. Open Prolog itself, or the System.
  214.  
  215. Speech Manager predicates.
  216.  
  217. Two predicates are available: speakAtom/1 and speechBusy/1.
  218. They both need the Speech Manager to be present and will throw error codes if it is not. 
  219. speakAtom/1 takes its atom and speaks it. You have no control over any of the parameters of speech. As soon as the command is sent to the Sound Manager, control returns to Open Prolog, which can then process while the speech is being uttered. If you execute another speakAtom, the previous speech will complete before the command is honoured.
  220. speechBusy/1 unifies its argument with the number of speech channels busy. If there is no speech, the number of channels will be 0.
  221. These are external predicates, and the code for them is given in the External Predicates… Folder.
  222.  
  223.  
  224. • Low Level Debug
  225.  
  226. macsbug & macsbugFail need Macsbug to work, and will crash otherwise.
  227.  
  228. • File Stuff
  229.  
  230. 'system$get$file$size'(FileName,FileSize,ErrorNumber).
  231. %returns in FileSize the size of FileName in bytes. (Normally one character occupies one byte.) An ErrorNumber of 0 is OK, anything else is a problem. Note that it isn't necessarily possible to open the file.
  232. e.g.
  233. 'system$get$file$size'('Open Prolog Info',FileSize,ErrorNumber).
  234. 'system$get$fpos'(Fpos).
  235.  
  236. %returns the ordinate (that is, the position in the file, starting at 0) of the next character to be read from the current input file.
  237.  
  238. 'open$prolog$eof$char'(CurrentEofChar,NewEofChar). %default is 26
  239. 'open$prolog$eoln$char'(CurrentEolnChar,NewEolnChar). %default 31
  240.  
  241. These are the characters returned by get or get0 for an end-of-line condition of an end-of-file condition, chosen for compatibility with DEC10 Prolog.
  242.  
  243. • Window-handling Predicates.
  244.  
  245. A number of predicates have been written to handle window-based I/O.
  246. Please be aware that they are subject to change.
  247.  
  248. Basic Ideas:
  249. °    Window Numbers
  250. We use window numbers because it is not possible to refer to a window by its name. This is because its name may change, and because (in theory anyway) many windows could have the same names.
  251. So, every window is assigned a window number when it is created (distinct from its parameter pointer or file number etc.). 
  252.  
  253. °    Starting Pointer & Single-Line-Selection
  254. Whenever text is output to a window (say using the write command), a pointer, called the Starting Pointer, is made point to the next character after the last
  255. character output.
  256.  
  257. When a user selects text using the ENTER key, the text selected goes
  258. into a text buffer that is passed to the Prolog interpreter.
  259. In the case of single-line-selection, the start of the selection is found by going from the caret backwards along the line to the start of the line or to the Starting Pointer, whichever comes first. The end of the selection is the end of the line. 
  260.  
  261. ° Highlighting & Outlines
  262. It is possible to highlight a particular run of text. Just to confuse you,
  263. the highlight is called an outline, because that's how it started out.
  264. The highlight will 'stick' to the text specified even when text before,
  265. within or after it has been added or removed. Therefore it's not safe to
  266. refer to a highlight by its start & end points, which may vary. Instead, a
  267. highlight is given a unique index number by which it may be referred to
  268. later.
  269.  
  270. ◊    Some Built-Ins
  271.  
  272. 'system$window$open$file$window'(Name,WindowNumber,ResultCode)
  273. A window is opened with the name given (or brought to the front if it's
  274. open already). A file of the same name must exist, and it's opened into the
  275. window. A result code of 0 indicates everything was OK;
  276. anything else means some error occured. Standard Macintosh
  277. error codes are used.
  278.  
  279. 'system$window$open$new$window'(Name,WindowNumber,ResultCode)
  280. A window is opened with the name given (or brought to the front if it's
  281. open already).
  282. No connection is made with a file of the same name even if it exists.
  283.  
  284. 'system$window$set$starting$point'(WindowNumber,StartingPoint) &
  285. 'system$window$get$starting$point'(WindowNumber,StartingPoint).
  286.  
  287. These can be used to get and set the starting point. Normally you needn't
  288. bother with these, as the starting point is automatically updated.
  289.  
  290. Sometimes, however, you 'll need them. For example, say you've written
  291. a parser, and you want to indicate errors to the user in the window.
  292. When you display the error message as appropriate, you'll want to
  293. reset the window environment just as it was just before
  294. the error occured. For this, you'll need to get the Starting Point beforehand,
  295. and then reset it after outputting the error message, etc.
  296. I have a rather complex example, so mail me if you'd like it.
  297.  
  298. 'system$window$front'(WindowNumber).
  299. Tells you which window is at the front.
  300.  
  301. 'system$window$get$output'(WindowNumber).
  302. Tells you which is the current output window.
  303.  
  304. 'system$window$set$output'(WindowNumber,Status).
  305. The designated window becomes the output window. Any error (e.g. if
  306. the window is read-only, etc. will be reported in Status. A Status of 0
  307. means OK.
  308.  
  309. 'system$window$select'(WindowNumber).
  310. Bring the window to the front.
  311.  
  312. •Note, in the following, where Start and End character offsets are mentioned, character numbers start at 0. The range from start to finish is inclusive of the starting character and exclusive of the ending character.
  313.  
  314. 'system$window$get$selection'(WindowNumber,Start,End)
  315. Tells you the selection start and end.
  316.  
  317. 'system$window$set$selection'(WindowNumber,Start,End)
  318. Allows you to set the selection start and end.
  319.  
  320. 'system$window$set$outline'(WindowNumber,StyleNumber,Start,End,OutlineReference).
  321. Only a style 1 is defined.
  322.  
  323. 'system$window$clear$outline'(WindowNumber,OutlineReference).
  324.  
  325. 'system$window$buffer$size'(WindowNumber,UnreadBytesLeftInSelection).
  326.  
  327. For example, to highlight the first 100 characters in this file:
  328.  
  329. 'system$window$front'(WindowNumber),
  330. 'system$window$set$outline'(WindowNumber,1,0,100,OutlineReference).
  331.